local yiqin = fk.CreateSkill{
  name = "wk_heg__yiqin",
}

Fk:loadTranslationTable{
  ["wk_heg__yiqin"] = "益亲",
  [":wk_heg__yiqin"] = "出牌阶段限一次，你可以展示牌堆顶X张牌并选择至多等量名角色（X为所有明置牌总花色数），然后这些角色依次获得其中一张牌并明置之。",
  ["#wk_heg__yiqin_target"] = "益亲：你可令至多等量角色依次获得其中一张牌并明置之",
}

local H = require "packages.ol_hegemony.util"
local HG = require "packages.new_heg_diy_my.util"

yiqin:addEffect("active",{
  can_use = function (self, player)
    local suits = {}
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      for _, c in ipairs(p:getCardIds("h")) do
        if Fk:getCardById(c):getMark(MarkEnum.ShownCards) > 0 then
          table.insertIfNeed(suits,Fk:getCardById(c).suit)
          if #suits >0 then break end
        end
      end
    end
    table.removeOne(suits, Card.NoSuit)
    return player:usedSkillTimes(yiqin.name, Player.HistoryPhase) == 0 and #suits > 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, use)
    local player = use.from
    local suits = {}
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      for _, c in ipairs(p:getCardIds("h")) do
        if Fk:getCardById(c):getMark(MarkEnum.ShownCards) > 0 then
          table.insertIfNeed(suits, Fk:getCardById(c).suit)
        end
      end
    end
    table.removeOne(suits, Card.NoSuit)
    local cards = room:getNCards(#suits)
    room:moveCards({
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonJustMove,
      proposer = use.from,
    })
    local targets = room:askToChoosePlayers(player,{
      targets = room:getAlivePlayers(),
      min_num = 1,
      max_num = #suits,
      prompt = "#wk_heg__yiqin_target",
      skill_name = yiqin.name})
    table.forEach(room:getAlivePlayers(), function(p) room:fillAG(p, cards) end)
    room:sortByAction(targets)
    for _, to in ipairs(targets) do
      if not to.dead then
        local chosen = room:askToAG(to, {
          id_list = cards,
          cancelable = false,
          skill_name = yiqin.name
        })
        room:moveCards{
          ids = {chosen},
          to = to,
          toArea = Player.Hand,
          moveReason = fk.ReasonPut,
        }
        room:takeAG(to, chosen)
        HG.showCards(to, chosen)
        table.removeOne(cards, chosen)
      end
    end
    table.forEach(room:getAlivePlayers(), function(p) room:closeAG(p) end)
    local toDiscard = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    room:moveCards{
      ids = toDiscard,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile
    }
  end
})

return yiqin